﻿namespace Bread.Utility;

public class MimeHelper
{
    // https://www.sitepoint.com/mime-types-complete-list/
    // https://docs.w3cub.com/http/basics_of_http/mime_types/complete_list_of_mime_types.html#google_vignette
    static Dictionary<string, string> GetTypes() => new Dictionary<string, string>
        {
            {".txt", "text/plain"},
            {".csv", "text/csv"},
            {".css", "text/css"},
            {".htm", "text/html"},
            {".html", "text/html"},
            {".config","text/plain" },
            {".js","text/javascript"},
            {".xml","text/xml" },

            {".pdf", "application/pdf"},
            {".epub", "application/epub+zip"},
            {".json", "application/json"},
            {".zip","application/x-compressed"},
            {".7z","application/x-7z-compressed"},
            {".tar","application/x-tar"},

            {".doc", "application/vnd.ms-word"},
            {".docx", "application/vnd.ms-word"},
            {".xls", "application/vnd.ms-excel"},
            {".xlsx", "application/vnd.openxmlformats officedocument.spreadsheetml.sheet"},
            {".ppt", "application/vnd.ms-powerpoint"},
            {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},

            {".exe", "application/octet-stream" },
            {".bin", "application/octet-stream" },
            {".dll", "application/octet-stream" },
            {".data", "application/octet-stream" },
            {".swf", "application/x-shockwave-flash" },
            {".sh", "application/x-sh" },

            {".png", "image/png"},
            {".jpg", "image/jpeg"},
            {".jpeg", "image/jpeg"},
            {".gif", "image/gif"},
            {".webp", "image/webp"},
            {".svg", "image/svg+xml"},
            {".apng", "image/apng"},
            {".tiff", "image/tiff"},

             {".aac", "audio/aac"},
             {".mp3", "audio/mpeg"},
             {".wav", "audio/wav"},
             {".flac", "audio/flac"},
             {".wma", "audio/wma"},
             {".ape", "audio/ape"},
             {".weba", "audio/webm"},
             {".opus", "audio/opus"},

             {".mp4", "video/mp4"},
             {".mpeg", "video/mpeg"},
             {".webm", "video/webm"},
             {".ts", "video/mp2t"},
             {".avi", "video/x-msvideo"},
             {".mkv", "video/x-matroska"},
             {".mov", "video/quicktime"},
             {".qt", "video/quicktime"},
             {".ogg", "video/ogg"},
             {".rv", "video/vnd.rn-realvideo"},
             {".ts", "video/mp2t"},
        };


    public static string GetMimeTypeByPath(string path)
    {
        if (string.IsNullOrEmpty(path)) throw new ArgumentException();
        string ext = System.IO.Path.GetExtension(path);
        return GetMimeTypeByExtension(ext);
    }

    static Dictionary<string, string>? _types = null;

    public static string GetExtensionByMimeType(string type)
    {
        if (string.IsNullOrEmpty(type)) throw new ArgumentException("type is null");
        type = type.ToLowerInvariant();
        if (_types == null) _types = GetTypes();
        foreach (var d in _types) {
            if (d.Value == type) {
                return d.Key;
            }
        }
        throw new KeyNotFoundException();
    }

    public static string GetMimeTypeByExtension(string extension)
    {
        if (string.IsNullOrEmpty(extension)) throw new ArgumentException("文件后缀名缺失");
        extension = extension.ToLowerInvariant();
        if (_types == null) _types = GetTypes();
        foreach (var d in _types) {
            if (d.Key == extension) {
                return d.Value;
            }
        }
        throw new NotImplementedException();
    }
}
